package cn.geracomium.action;

import cn.geracomium.util.action.AbstractAction;
import cn.gov.service.IBedService;
import cn.gov.vo.Bed;
import cn.gov.vo.Building;
import cn.gov.vo.Floor;
import cn.gov.vo.Room;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * Created by {wang jian} on 2018/06/24.
 */
@Controller
public class BedAction extends AbstractAction{

    @Reference
    private IBedService service;
    @RequestMapping("gotoBed")
    public String gotoBed(){
        return "files/bed";
    }
    @RequestMapping("get")
    public ModelAndView get(){
        ModelAndView mav = new ModelAndView("files/bed");
        System.err.println(this.service.getAllBuilding());
        mav.addObject("allBuilding",this.service.getAllBuilding());
        return mav ;
    }
    @RequestMapping("bed_manage")
    public ModelAndView get1(){
        ModelAndView mav = new ModelAndView("pages/files/server/bed_manage");
        System.err.println(this.service.getAllBuilding());
        mav.addObject("allBuilding",this.service.getAllBuilding());
        return mav ;
    }
    @RequestMapping("bed_add")
    public ModelAndView bedAdd(){
        ModelAndView mav = new ModelAndView("pages/files/server/bed_add");
        return mav ;
    }
    @RequestMapping("getFloor")
    @ResponseBody
    public Object getFloor(int buildingId){
        Object obj = this.service.getFloorByBuilding(buildingId) ;
        System.err.println(obj);
        return  obj;
    }
    @RequestMapping("getRoom")
    @ResponseBody
    public Object getRoom(int floorId){
        Object obj = this.service.getRoomByFloor(floorId) ;
        System.err.println(obj);
        return  this.service.getRoomByFloor(floorId);
    }
    @RequestMapping("getBed")
    @ResponseBody
    public Object getBed(int roomId){
        Object obj = this.service.getBedByRoom(roomId) ;
        System.err.println(obj);
        return  this.service.getBedByRoom(roomId);
    }


    @RequestMapping("save_building")
    @ResponseBody
    public Object saveBuilding(HttpServletRequest request, HttpServletResponse response){
        Building b = new Building();
        String name = (String)request.getParameter("names");
        b.setName(name);
        b.setServiceStationCode("123");
        return  this.service.addBuilding(b);
    }

    @RequestMapping("save_floor")
    @ResponseBody
    public Object saveFloor(Floor vo){
        vo.setServiceStationCode("123");
        System.err.println(vo);
        return  this.service.addFloor(vo);
    }
    @RequestMapping("save_room")
    @ResponseBody
    public Object saveRoom(Room vo){
        vo.setServiceStationCode("123");
        System.err.println(vo);
        return  this.service.addRoom(vo);
    }
    @RequestMapping("save_bed")
    @ResponseBody
    public Object saveBed(Bed vo){
        vo.setServiceStationCode("123");
        System.err.println(vo);
        return  this.service.addBed(vo);
    }
    @RequestMapping("add_batch")
    @ResponseBody
    public Object addBatch(String buildingName,int floorNumber,int roomNumberPerFloor,int bedNumberPerRoom,int price){
        Building build = new Building() ;
        Floor floor = null ;
        Room room = null ;
        Bed bed = null ;
        build.setName(buildingName);
        build.setServiceStationCode("123");
        this.service.addBuilding(build);
        int buildId = this.service.getBuildIdByName(buildingName);

        for(int x = 0 ; x < floorNumber ; x ++){
            floor = new Floor();
            floor.setServiceStationCode("123");
            floor.setBuildingId(buildId);
            String floorName = buildingName+":floor"+x;
            floor.setName(floorName);
            this.service.addFloor(floor);
            int floorId = this.service.getFloorIdByName(floorName);
            for (int i = 0 ;i < roomNumberPerFloor ; i ++ ){
                room = new Room();
                room.setServiceStationCode("3");
                room.setFloorId(floorId);
                String roomName = floorName + ":room" + i ;
                room.setName(roomName);
                this.service.addRoom(room);
                int roomId = this.service.getRoomIdByName(roomName);
                for(int y=0 ; y < bedNumberPerRoom ; y ++ ){
                    bed = new Bed();
                    bed.setRoomId(roomId);
                    bed.setServiceStationCode("123");
                    String bedName = roomName + ":bed"+ y ;
                    bed.setName(bedName);
                    bed.setPrice(price);
                    HashMap<String,Object> map = (HashMap<String,Object>)this.service.addBed(bed);
                    System.err.println(map);
                }
            }

        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("resultCode","SUCC");
        return map  ;
    }


    @RequestMapping("update_building")
    @ResponseBody
    public Object updateBuilding(HttpServletRequest request, HttpServletResponse response,String names,Building b){
        String name = (String)request.getParameter("names");
        System.err.println(b);
        b.setServiceStationCode("123");
        b.setName(name);
        return  this.service.edit(b);
    }
    @RequestMapping("update_floor")
    @ResponseBody
    public Object updateFloor(Floor  b){
        b.setServiceStationCode("123");
        System.err.println(b);
        return  this.service.editFloor(b);
    }
    @RequestMapping("update_bed")
    @ResponseBody
    public Object updateBed(Bed  b){
        b.setServiceStationCode("123");
        System.err.println(b);
        this.service.editBed(b);
        HashMap map = new HashMap<>();
        map.put("resultCode","SUCC");
        return  map;
    }
    @RequestMapping("del_building")
    @ResponseBody
    public Object delBuilding(HttpServletRequest request,Building b){
        b.setServiceStationCode("123");
        System.err.println(b.getId());
        System.err.println(b.getId());
        System.err.println(b.getId());
        return  this.service.del(b.getId());
    }
    @RequestMapping("del_floor")
    @ResponseBody
    public Object delFloor(int id){
            System.err.println(id);
       return  this.service.delFloor(id);
    }
    @RequestMapping("del_room")
    @ResponseBody
    public Object delRoom(int id){
        System.err.println(id);
        return  this.service.delRoom(id);
    }
    @RequestMapping("del_bed")
    @ResponseBody
    public Object delBed(int id){
        System.err.println(id);
        return  this.service.delBed(id);
    }
    @RequestMapping("get_room")
    @ResponseBody
    public Object getRoom1(int id){
        System.err.println(id);
        return  this.service.getRoomById(id);
    }




}

